package com.merry.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.aspectj.weaver.ast.Var;

import java.io.FileInputStream;
import java.util.Iterator;

/**
 * @author zouyb
 *  Excel的读取可以分为： 读和写不一样的地方在于，写不需要遍历这个集合，读需要遍历这个集合。
 *      1.通过文件流读取Excel工作簿
 *      2.获取工作簿
 *      3.获取表
 *      4.获取行
 *      5.获取单元格
 *      6.读取数据
 *      7.关闭流
 */
public class ExcelReadDemo {

    public static void main(String[] args) throws Exception {
        ExcelReadDemo excelReadDemo = new ExcelReadDemo();
        excelReadDemo.readExcel03();
        excelReadDemo.readExcel07();
    }


    public void readExcel03() throws Exception{
        FileInputStream fileInputStream = new FileInputStream("./03版本测试.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        while (sheetIterator.hasNext()){
            Sheet sheet = sheetIterator.next();
            Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()){
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()){
                    Cell cell = cellIterator.next();
                    System.out.print(cell.getStringCellValue());
                }
            }
        }
    }

    public void readExcel07() throws Exception{
        FileInputStream fileInputStream = new FileInputStream("./07版本测试.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
        while (sheetIterator.hasNext()){
            Sheet sheet = sheetIterator.next();
            Iterator<Row> rowIterator = sheet.rowIterator();
            while (rowIterator.hasNext()){
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()){
                    Cell cell = cellIterator.next();
                    System.out.print(cell.getStringCellValue());
                }
            }
        }
    }
}
